package com.yune.utils;
import android.annotation.SuppressLint;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Network {
    static String id="20448ed1320e6a6c2c21a387f58ea20b";
    static String key="f1401c2e284bc7f8baf23785b11bbd47";
    static String mkey="b48781d32280fdaec55f0f5683a18805";
    public static void get(String url, NetWorkCallback c){
        final Request.Builder builder=new Request.Builder().url(url);
        builder.addHeader("X-Bmob-Application-Id",id);
        builder.addHeader("X-Bmob-REST-API-Key",key);
        builder.addHeader("X-Bmob-Master-Key",mkey);
        builder.addHeader("Content-Type","application/json");
        final Request request=builder.build();
        final OkHttpClient client=new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).connectTimeout(10, TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();
        final Call call=client.newCall(request);
        new Thread(()->{
            try {
                Response response=call.execute();
                assert response.body() != null;
                if (c!=null){
                    c.callback(response.body().string());
                }
            } catch (Exception ignored) {
            }
        }).start();
    }
    public static void post(JSONObject jsonObject, String url, NetWorkCallback c){
        new Thread() {
            @SuppressLint("SetTextI18n")
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject));
                Request.Builder request = new Request.Builder().url(url);
                request.addHeader("X-Bmob-Application-Id",id);
                request.addHeader("X-Bmob-REST-API-Key",key);
                request.addHeader("X-Bmob-Master-Key",mkey);
                request.addHeader("Content-Type","application/json");
                request.post(requestBody);
                final OkHttpClient client=new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).connectTimeout(10,TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();
                final Call call=client.newCall(request.build());
                new Thread(() -> {
                    try {
                        Response response=call.execute();
                        assert response.body() != null;
                        if (c!=null){
                            c.callback(response.body().string());
                        }
                    } catch (Exception ignored) { }
                }).start();
            }
        }.start();
    }
    public static void delete(String url, NetWorkCallback c){
        new Thread() {
            @SuppressLint("SetTextI18n")
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                Request.Builder request = new Request.Builder().url(url);
                request.addHeader("X-Bmob-Application-Id",id);
                request.addHeader("X-Bmob-REST-API-Key",key);
                request.addHeader("X-Bmob-Master-Key",mkey);
                request.addHeader("Content-Type","application/json");
                request.delete();
                final OkHttpClient client=new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).connectTimeout(10,TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();
                final Call call=client.newCall(request.build());
                new Thread(() -> {
                    try {
                        Response response=call.execute();
                        assert response.body() != null;
                        if (c!=null){
                            c.callback(response.body().string());
                        }
                    } catch (Exception ignored) {
                    }
                }).start();
            }
        }.start();
    }
    public static void put(JSONObject data, String url, NetWorkCallback c){
        new Thread() {
            @SuppressLint("SetTextI18n")
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(data));
                OkHttpClient okHttpClient =new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).connectTimeout(10,TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();
                Request request = new Request.Builder()
                        .url(url)
                        .put(requestBody)
                        .addHeader("X-Bmob-Application-Id",id)
                        .addHeader("X-Bmob-REST-API-Key",key)
                        .addHeader("X-Bmob-Master-Key",mkey)
                        .addHeader("Content-Type","application/json")
                        .build();
                Call call=okHttpClient.newCall(request);
                new Thread(() -> {
                    try {
                        Response response=call.execute();
                        assert response.body() != null;
                        if (c==null)return;
                        c.callback(response.body().string());
                    } catch (Exception e) {
                        if (c==null)return;
                        try {
                            c.callback(null);
                        } catch (JSONException jsonException) {
                            jsonException.printStackTrace();
                        }
                    }
                }).start();
            }
        }.start();
    }
    public static void callApi(String url,Callback callback ){//在find中 type1是搜索,2是获取歌曲播放地址，music中1是获取歌词
        okhttp3.Request request = new okhttp3.Request.Builder().url(url).get().build();
        new OkHttpClient().newCall(request).enqueue(callback);
    }
}